
package com.probiz.estore.imports.handler.product;

import org.apache.log4j.Logger;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.attribute.Attribute;
import com.probiz.estore.common.model.attribute.ProductAttrValue;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductAttGroup;
import com.probiz.estore.common.model.product.ProductAttGroupItem;
import com.probiz.estore.common.service.AttributeService;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;
import com.probiz.estore.product.service.ProductAttGroupItemManager;

public class AttributeHandler extends ColumnBasicHandler implements	ColumnHandler {
	private Logger logger = Logger.getLogger(AttributeHandler.class);
	private ProductAttGroupItemManager productAttGroupItemManager;
	private AttributeService attributeService;
	public void setProductAttGroupItemManager(ProductAttGroupItemManager productAttGroupItemManager) {
		this.productAttGroupItemManager = productAttGroupItemManager;
	}
	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}
	
	@SuppressWarnings("unchecked")
	public void setProperty(ImportModel importModel, Column column) throws Exception {
		Product product=(Product)importModel.getTarget();
		String value = column.getValue();
		String attributeName = column.getRealHeader().substring(column.getColumnHeader().length(), column.getRealHeader().length());
		boolean flag = false;
		stop:for(ProductAttGroup group : product.getProductType().getProductAttGroups()){
			for(ProductAttGroupItem item : group.getProductAttGroupItems()){
				Attribute attribute = item.getAttribute();
				if(attribute.getStatus()!=null && Constants.FLAG_TRUE.equals(attribute.getStatus())){
					if(attributeName.equals(attribute.getAttributeName())){
						//找到匹配的属性
						ProductAttrValue productAttrValue = null;
						if(null != product.getProductId()){
							productAttrValue=attributeService.getProductAttributeValue(product.getId(), attribute.getId());
						}
						if(null==productAttrValue){
							productAttrValue = new ProductAttrValue();
							productAttrValue.setAttributeDataType(attribute.getAttributeDataType());
							productAttrValue.setAttribute(attribute);
							productAttrValue.setProductAttGroupItem(item);
						}
						productAttrValue.setAttributeValue(value);
						product.getImportAttributeValueList().add(productAttrValue);
						flag = true;
						break stop;
					}
				}
			}
		}
		if(flag){
			importModel.setResult(ImportModel.RESULT_SUCCESS);
		}else{
			//设置结果
			importModel.setResult(ImportModel.RESULT_ERROR);
			String msg = "无法导入，在商品所属商品类型的关联属性下找不到商品属性【"+attributeName+"】，或该属性未激活";
			logger.error(msg);
			importModel.setMsg(msg);
		}
		
	}


	

}
